package com.xuan.chatroom.interceptor;

import com.xuan.chatroom.constants.Constant;
import com.xuan.chatroom.model.User;
import com.xuan.chatroom.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();

        // 在方法执行前进行拦截，此处判断哪些方法可以被执行
        // 从 header中的token 判断用户是否登录
        String token = request.getHeader(Constant.USER_TOKEN_HEADER);
        if (token == null || JwtUtil.parseToken(token) == null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
